package com.danco.sn.model.dao;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.danco.sn.apientities.Entities;

public abstract class GenericDAO<T extends Entities> {

	private Class<T> currentClass;

	protected GenericDAO(Class<T> cl) {
		this.currentClass = cl;
	}

	public void update(Entities o, Session session) throws HibernateException {
		session.update(o);
	}

	public void create(Entities e, Session session) throws HibernateException {
		session.save(e);
	}

	public void delete(Entities o, Session session) throws HibernateException {
		session.delete(o);
	}

	public Entities read(String propertyName, Object value, Session session)
			throws HibernateException {
		Criteria criteria = session.createCriteria(currentClass).add(
				Restrictions.eq(propertyName, value));
		Entities entity = (Entities) criteria.uniqueResult();
		return entity;
	}

}
